package main.java.managers;

import main.java.entities.Historial;
import main.java.factory.SystemFactory;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class HistoriaMgr {

	//TODO: implementar todos los metodos necesarios para obtener los datos.
	
	/*
	 * Buscar driver para la conexion con la base de datos. Guardar los strings de conexion en database.properties.
	 * getUltimaMedidaDeCadaSensor():List o Map Medidor->sensor->temperatura
	 * getHistorialSensor(fecha inicio o cantidad de medidas para atras): lista de medidas
	 * 
	 * etc etc (todas las que te parezcan necesarias (cada una con javadoc de que es lo que hace y que es lo que devuelve)
	 * Implementar todas las clases o datatypes que sean necesarios
	 * 
	 * 
	 * */
	public List<Historial> getLastMeasures(){
		List<Historial> lh = new ArrayList<Historial>();
		try {
			Class.forName(SystemFactory.getInstance().getDriver());
			Connection con = DriverManager.getConnection(SystemFactory.getInstance().getUrl(), SystemFactory.getInstance().getUser(), SystemFactory.getInstance().getPasswd());
			PreparedStatement prep = con.prepareStatement("select max(historial_id) as historial_id,timestamp,register_type,address,grd_id,value,historical_type from historical group by address");
			prep.execute();
			ResultSet r = prep.getResultSet();
			
			
			while (r.next()){
				int historial_id = r.getInt("historial_id");
				int grd_id = r.getInt("grd_id");
				int register_type = r.getInt("register_type"); 	
				Timestamp timestamp = r.getTimestamp("timestamp");
				int address = r.getInt("address");
				int value = r.getInt("value");
				int historical_type = r.getInt("historical_type");
				Historial h = new Historial(historial_id, grd_id, register_type, timestamp, address, value, historical_type);
				lh.add(h);
			}
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return lh;
	}
	
	public List<Historial> getHistoricalForSensor(String sensor){
		List<Historial> lh = new ArrayList<Historial>();
		try {
			Class.forName(SystemFactory.getInstance().getDriver());
			Connection con = DriverManager.getConnection(SystemFactory.getInstance().getUrl(), SystemFactory.getInstance().getUser(), SystemFactory.getInstance().getPasswd());
			PreparedStatement prep = con.prepareStatement("select MAX(historial_id) AS historial_id ,timestamp ,register_type,address,grd_id,value,historical_type from historical where address = "+sensor+" group by timestamp order by historial_id limit 20");
			prep.execute();
			ResultSet r = prep.getResultSet();
			
			
			while (r.next()){
				int historial_id = r.getInt("historial_id");
				int grd_id = r.getInt("grd_id");
				int register_type = r.getInt("register_type"); 	
				Timestamp timestamp = r.getTimestamp("timestamp");//Date("timestamp");
				int address = r.getInt("address");
				int value = r.getInt("value");
				int historical_type = r.getInt("historical_type");
				Historial h = new Historial(historial_id, grd_id, register_type, timestamp, address, value, historical_type);
				lh.add(h);
			}
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return lh;
	}
	
}
